modalController.$inject = ['$scope', '$location', 'merchantService', 'merchant', '$uibModalInstance', 'changeStatus'
];
function modalController($scope, $location, merchantService, merchant, $modalInstance, changeStatus) {
    $scope.merchant = merchant;
    var vm = this;

    vm.merchant = [];
    $scope.cancel = function () {
        console.log("close the modal");
        $modalInstance.dismiss('cancel');
    }

    $scope.operate = function (status) {
        console.log("enter the pass method");
        let oldStatus=merchant.status;
        merchant.status = status;
        merchantService.updateStatus(merchant, function (err, result) {
            if (err) {
                console.log("error " + err.message);
                $scope.merchant.status=oldStatus;
            }
            else {
                console.log(result);
                if (result.nModified == 1 && result.ok == 1) {
                    console.log("success to call back");
                    alert("成功修改" + merchant.username + "商家的状态");
                    $scope.cancel();
                    changeStatus(status);
                }
                else {
                    console.log("fail to update");
                    alert("sorry,出错了");
                    $scope.merchant.status=oldStatus;
                    $scope.cancel();
                }
            }
        })
    };

    $scope.reject = function (failReason) {
         let oldStatus=merchant.status;
        if (!failReason) {
            $scope.error = "请填写驳回理由";
        } else {
            console.log("enter the reject method");
            merchant.status = 3;
            merchantService.updateStatus(merchant, function (err, result) {
                if (err) {
                    console.log("error " + err.message);
                    $scope.merchant.status=oldStatus;
                }
                else {
                    console.log(result);
                    if (result.nModified == 1 && result.ok == 1) {
                        console.log("success to call back");
                        alert("成功修改" + merchant.username + "商家的状态");
                        $scope.cancel();
                        changeStatus(3);
                    }
                    else {
                        console.log("fail to update");
                        alert("sorry,出错了");
                        $scope.merchant.status=oldStatus;
                        $scope.cancel();
                    }
                }
            })
        }
    };
}

module.exports = modalController;

